/**
 * 
 */
package com.alstudio.ui.module.voip;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.pojo.module.local.CallPrice;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.account.UserProfileManager;
import com.alstudio.utils.android.color.ALColorUtils;
import com.alstudio.view.ViewUtils;
import com.alstudio.view.tableview.ALBasicListItem;
import com.alstudio.view.tableview.ALTableView;
import com.alstudio.view.tableview.ALTableView.ALTableViewClickListener;
import com.alstudio.view.tableview.ViewItem;
import com.loovee.imaohu.R;

/**
 * 收费设置
 * 
 * @author alonso lee
 * 
 */
public class SetVoipCallPriceActivity extends TitleBarActivity implements
		ALTableViewClickListener {

	private ALTableView tableView;
	private ArrayList<CallPrice> prices = new ArrayList<CallPrice>();
	private ArrayList<ALBasicListItem> items = new ArrayList<ALBasicListItem>();

	private int selectedIndex = -1;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(getApplicationContext(), getClass().getSimpleName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		requeqtData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setCenterTextViewMessage(R.string.TxtSetVoipCallPirceTitle);
		setTitleBarActivityContentView(R.layout.set_voip_call_price_layout);
		tableView = (ALTableView) findViewById(R.id.aLTableView1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#requeqtData()
	 */
	@Override
	public void requeqtData() {
		// TODO Auto-generated method stub
		super.requeqtData();
		if (isDataReceived)
			return;
		if (ALLocalEnv.isSendXmppDataEnable()) {
			showLoadingProgress();
			ALUiXmppRequestCreator.postALUiXmppVoipCheckCallPrice();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleCheckVoipCallPriceListEvent(com
	 * .alstudio.pojo.ALXmppEvent)
	 */
	@Override
	public void handleCheckVoipCallPriceListEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleCheckVoipCallPriceListEvent(data);
		isDataReceived = true;
		dismissLoadingProgress();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			@SuppressWarnings("unchecked")
			ArrayList<CallPrice> tmp = (ArrayList<CallPrice>) data.getData();
			prices.addAll(tmp);
			tmp.clear();
			tmp = null;
			initTableView();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleSetVoipCallPriceEvent(com.alstudio
	 * .pojo.ALXmppEvent)
	 */
	@Override
	public void handleSetVoipCallPriceEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleSetVoipCallPriceEvent(data);
		dismissLoadingProgress();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			// ALLocalEnv.getLocalEnv().getMyProfile().setCallPrice(callPrice);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#finish()
	 */
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		items.clear();
		items = null;
	}

	/**
	 * 
	 * @param parent
	 */
	private void initTableView() {
		tableView.setClickListener(this);

		for (CallPrice price : prices) {
			String title = "";
			title = getString(R.string.TxtSetVoipCallPirceItem);
			title = String.format(title, price.getPrice(), price.getPoint());
			if (!TextUtils.isEmpty(price.getPrice())) {
				if (price.getPrice().equals("0")) {
					title = getString(R.string.TxtFree);
				}
			}

			ALBasicListItem item = new ALBasicListItem(getApplicationContext());
			item.title.setText(title);
			item.setChevronImg(R.drawable.single_choice_on);
			ViewUtils.goneView(item.chevron);

			ViewItem i = new ViewItem(item);
			tableView.addViewItem(i);
			items.add(item);

		}

		tableView.commit();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.tableview.ALTableView.ALTableViewClickListener#onClick
	 * (int)
	 */
	@Override
	public void onClick(int index) {
		// TODO Auto-generated method stub
		if (ALLocalEnv.isSendXmppDataEnable()) {
			if (selectedIndex == -1 || selectedIndex != index) {
				selectedIndex = index;
				showLoadingProgress();
				CallPrice price = prices.get(index);
				ALLocalEnv.getLocalEnv().getMyProfile()
						.setCallPrice(price.getPrice());
				UserProfileManager.saveMyProfile(ALLocalEnv.getLocalEnv()
						.getMyProfile());
				ALUiXmppRequestCreator.postALUiXmppVoipSetCallPrice(price
						.getPrice());
				updateTableView(index);
			}
		}
	}

	public void updateTableView(final int index) {
		mhandler.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				for (int i = 0; i < items.size(); i++) {
					ALBasicListItem item = items.get(i);
					if (i == index) {
						item.setTitleColor(ALColorUtils
								.getColor(getApplicationContext(),
										R.color.base_app_color));
						ViewUtils.showView(item.chevron);
					} else {
						item.setTitleColor(ALColorUtils.getColor(
								getApplicationContext(),
								R.color.text_color_selector));
						ViewUtils.goneView(item.chevron);
					}
				}
			}
		});

	}

}
